﻿#include "preferences.h"
#include "ui_preferences.h"
#include <QMessageBox>
#include <QApplication>
#include <QDesktopWidget>
#include <QTextDocumentWriter>
#include "widget.h"

//---------------------------------------------------------------------------
Preferences::Preferences (SessionManager* sessionManager, CenteredWidget *parent) :
    QDialog(parent),
    ui(new Ui::Preferences)
{
    m_config = new Config (0);
    m_fileDialog = new QFileDialog (this);
    m_sessionManager = sessionManager;

    setModal (false);
    ui->setupUi(this);

    m_colorSelectButton = new ColorSelectButton (this);
    ui->colorSelectLayout->addWidget (m_colorSelectButton);

    ui->optionsClose->setIcon (QIcon::fromTheme ("window-close", QIcon (":/images/window-close.svg")));
    ui->findPath->setIcon (QIcon::fromTheme ("edit-find", QIcon (":/images/edit-find.svg")));

    readGlobalOptions ();   //--------------------------


    connect (ui->optionsToolBox,
             SIGNAL(currentChanged(int)),
             m_config,
             SLOT(setOptionsToolboxPage(int)));

    connect(ui->optionRestore,
            SIGNAL(clicked(bool)),
            m_config,
            SLOT(setOptionRestoreWorkSession(bool)));

    connect (ui->optionRememberGeometry,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionRememberGeometry(bool)));

    connect(ui->optionSimpleAnim,
            SIGNAL(clicked(bool)),
            m_config,
            SLOT(setOptionSimpleAnimation(bool)));

    connect(ui->optionReplaceCRBySpace,
            SIGNAL(clicked(bool)),
            m_config,
            SLOT(setOptionReplaceCRBySpace(bool)));

    connect (ui->optionRemoveTemp,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionRemoveTemp(bool)));

    connect (ui->optionUseXSane,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionUseXSane(bool)));

    connect (ui->optionUsePdfToText,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionUsePdfToText(bool)));

    connect (ui->optionAllSaneOptions,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionAllSaneOptions(bool)));

    connect (ui->optionAutoSaveScannedFiles,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionAutoSaveScannedFiles (bool)));

    connect (ui->optionResolutionForPdf,
             SIGNAL(valueChanged(int)),
             m_config,
             SLOT(setOptionResolutionForPdf(int)));

    connect (ui->optionRemoveCaesura,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionRemoveCaesura(bool)));

    connect (ui->optionSendTextTo,
             SIGNAL(currentIndexChanged(int)),
             m_config,
             SLOT(setOptionWordProcessorIndex(int)));

    connect (ui->optionDontAskForWP,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionDontAskForWP(bool)));

    connect (ui->optionCRBetweenROI,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionCRBetweenROI(bool)));

    connect (ui->optionExportTextToODF,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionExportTextToODF(bool)));

    connect (ui->optionInvertOrphanPixels,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionInvertOrphanPixels(bool)));

    connect (ui->optionInvertOrphanPixels,
             SIGNAL(clicked(bool)),
             ui->optionMinPixNeighbours,
             SLOT(setEnabled(bool)));

    connect (ui->optionMinPixNeighbours,
             SIGNAL(valueChanged(int)),
             m_config,
             SLOT(setOptionMinPixNeighbours(int)));

    connect (m_colorSelectButton,
             SIGNAL(colorChanged(QColor)),
             m_config,
             SLOT (setOptionUnderlineColor(QColor)));

    connect (ui->optionInvertColors,
             SIGNAL(clicked(bool)),
             m_config,
             SLOT(setOptionInvertColors(bool)));

    ui->optionsToolBox->setCurrentIndex (m_config->optionsToolboxPage ());
}
//---------------------------------------
Preferences::~Preferences()
{
    delete ui;
}
//---------------------------------------
void Preferences::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
//---------------------------------------
void Preferences::readGlobalOptions()
{
    QList<AppInfo> wpList (Config::wordProcessors ());
    int temp = m_config->optionWordProcessorIndex ();
    ui->optionSendTextTo->clear ();
    foreach (AppInfo info, wpList)
    {
        ui->optionSendTextTo->addItem (info.icon (), info.name ());
    }
    ui->optionSendTextTo->setCurrentIndex (temp);
    ui->optionSendTextTo->adjustSize ();

    ui->optionRestore->setChecked (m_config->optionRestoreWorkSession ());
    ui->optionRememberGeometry->setChecked (m_config->optionRememberGeometry ());
    ui->optionSimpleAnim->setChecked (m_config->optionSimpleAnimation ());
    ui->optionReplaceCRBySpace->setChecked (m_config->optionReplaceCRBySpace ());
    ui->optionRecentFilesMax->setValue (m_config->optionRecentFileMax ());
    ui->optionRemoveTemp->setChecked (m_config->optionRemoveTemp ());
    ui->optionUseXSane->setChecked (m_config->optionUseXSane ());
    ui->optionUsePdfToText->setChecked (m_config->optionUsePdfToText ());
    ui->optionAllSaneOptions->setChecked (m_config->optionAllSaneOptions ());
    ui->optionAutoSaveScannedFiles->setChecked (m_config->optionAutoSaveScannedFiles ());
    ui->optionPathForScannedFiles->setText (m_config->optionPathForScannedFiles ());
    ui->optionResolutionForPdf->setValue (m_config->optionResolutionForPdf ());
    ui->optionRemoveCaesura->setChecked (m_config->optionRemoveCaesura ());
    ui->optionDontAskForWP->setChecked (m_config->optionDontAskForWP ());
    ui->optionCRBetweenROI->setChecked (m_config->optionCRBetweenROI ());
    ui->optionExportTextToODF->setEnabled (m_config->ODFSupport ());
    ui->optionExportTextToODF->setChecked (m_config->optionExportTextToODF ());
    ui->optionInvertOrphanPixels->setChecked (m_config->optionInvertOrphanPixels ());
    ui->optionMinPixNeighbours->setValue (m_config->optionMinPixNeighbours ());
    ui->optionMinPixNeighbours->setEnabled (ui->optionInvertOrphanPixels->isChecked ());
    ui->optionInvertColors->setChecked (m_config->optionInvertColors ());
    m_colorSelectButton->setColor (m_config->optionUnderlineColor ());

    on_optionAutoSaveScannedFiles_clicked (m_config->optionAutoSaveScannedFiles ());
}
//--------------------------------------------
void Preferences::on_findPath_clicked()
{
    static QString directory  = ui->optionPathForScannedFiles->text ();


    m_fileDialog->setFileMode (QFileDialog::Directory);
    m_fileDialog->setOption (QFileDialog::ShowDirsOnly);
    m_fileDialog->setAcceptMode (QFileDialog::AcceptOpen);
    m_fileDialog->setViewMode (QFileDialog::List);
    m_fileDialog->setDirectory (directory);
    m_fileDialog->setWindowTitle (tr ("Choisir un répertoire..."));

    if (m_fileDialog->exec () == QDialog::Rejected) return;

    QString path = m_fileDialog->selectedFiles ().first ();
    ui->optionPathForScannedFiles->setText (path);
    ui->optionPathForScannedFiles->setFocus ();
    directory = path;
}
//--------------------------------------------------------------------
void Preferences::on_optionAutoSaveScannedFiles_clicked (bool checked)
{
    ui->optionPathForScannedFiles->setEnabled (checked);
    ui->findPath->setEnabled (checked);
    m_config->setOptionAutoSaveScannedFiles (checked);
}
//-------------------------------------------------------------
void Preferences::on_optionPathForScannedFiles_editingFinished ()
{
    QString path (ui->optionPathForScannedFiles->text ());

    if (!path.endsWith (QDir::separator ()))
        path += QDir::separator ();

    QDir dir;
    if (!dir.mkpath (path)) {
        QMessageBox::critical (this,
                               tr ("Erreur"),
                               tr ("Impossible de créer le répertoire\n%1").arg (path));
    }
    else {
        m_config->setOptionPathForScannedFiles (path);
    }
}
//------------------------------------------
void Preferences::on_optionsClose_clicked()
{
    m_config->sync ();
    on_optionPathForScannedFiles_editingFinished ();
    accept ();
}
//------------------------------------------------------
void Preferences::on_optionClearRecentFileList_clicked()
{
    if (!userAgrees (tr ("Cette action effacera l'historique des sessions"))) return;
    m_sessionManager->clear ();
}
//-------------------------------------------------------------
void Preferences::on_optionRecentFilesMax_valueChanged (int val)
{
    m_config->setOptionRecentFileMax (val);
}
//-----------------------------------------------
bool Preferences::userAgrees (const QString &text)
{
    return QMessageBox::warning (
                this,
                "SolidROC",
                text,
                QMessageBox::Ok | QMessageBox::Cancel,
                QMessageBox::Cancel
                ) == QMessageBox::Ok;
}
